import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import {
  registerApplication,
  start
} from 'single-spa'

Vue.config.productionTip = false;

async function loadScript(url){
  return new Promise((resolve, rejecet)=>{
    let script  = document.createElement('script')
    script.src = url
    script.onload = resolve
    script.onerror = rejecet
    document.body.appendChild(script)
  })
}

// singleSpa缺陷：不够灵活 不能加载动态js文件
// 样式不隔离 没有js沙箱机制

// 注册子应用
registerApplication(
  'vueApp',
  async () => {
      // console.log('加载vue模块');
      // systemJS 加载浏览器的模块
      await loadScript('http://localhost:10000/js/chunk-vendors.js')
      await loadScript('http://localhost:10000/js/app.js')
      return window.singleVue // 调用bootstrap mount unmount 方法
    },
    // 用户切换到/vue路径下，加载刚刚定义的子应用
    location => location.pathname.startsWith('/vue'),
  {a:1} // 数据传递 
)
start()

new Vue({
  router,
  render: h => h(App)
}).$mount("#app");